import base64
import json
import os
import shutil
import time
import random
from collections import OrderedDict
from urllib.parse import quote_plus

import redis
import requests
from sqlalchemy import create_engine
import pandas as pd
from DrissionPage import ChromiumPage
from DrissionPage import ChromiumOptions
from DrissionPage.common import Settings
from DrissionPage.common import Keys
from DrissionPage.common import Actions
import pymysql
# from DrissionPage.errors import *
# from xhs_detail_up import *
# import datetime




"""
https://customer.xiaohongshu.com/api/cas/customer/web/qr-code?service=https:%2F%2Fcreator.xiaohongshu.com&qr_code_id=68c517395516719772176435"""

class XHS_XPIDER:
    def __init__(self,user_id):
        self.CO = ChromiumOptions()  ##创建chrome实例
        # self.user_id = self.user_input()  ##用户
        self.user_id = user_id  ##用户

        self.PORT = self.CO.auto_port()  ##chrome端口
        # self.CO.set_paths(browser_path="browser_path='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")  ##chrome启动路径
        # self.CO.set_local_port(self.PORT)  ##生成的端口 这里是手动生成
        self.CO.set_user_data_path(os.getcwd() + '\\user_info_dir1\\%s'%self.user_id)  ##用户chrome文件夹
        print(self.CO)
        # self.CO.arguments.append('--headless')
        self.ch = ChromiumPage(self.CO)
        print(self.ch)


        # self.ch.set.window.max()  ##chrome窗口最大化
        # self.ac = Actions(self.ch)
        # self.fans = self.xhs_user_info_requests()
        print(self.user_id)
        # self.redis_conn = redis.StrictRedis(host='r-bp162522qrwjh5e7jhpd.redis.rds.aliyuncs.com', port=6379,
        #                                     password='123sdmmh568%$#vs*!', db=22)



    """爬虫主程序"""
    def xhs_huizong(self):
        # logging.debug(f"Input user={self.user_id}, port={self.PORT}")
        # logging.info(f"CURRENT COMPUTING MACHINE={'B'}")
        # self.ch.get('https://creator.xiaohongshu.com/creator/notes')
        # self.ch.get('https://www.baidu.com')
        time.sleep(5)
        self.ch.get('https://creator.xiaohongshu.com/new/home')
        time.sleep(3)
        if 'new/home' not in self.ch.url:
            self.ch.get('https://creator.xiaohongshu.com/new/home')

        # self.ch.url == 'https://creator.xiaohongshu.com/new/home'
        # self.ch.close()
        # self.ch.get('https://creator.xiaohongshu.com/new/home')


        # self.ch.url='https://creator.xiaohongshu.com/new/home'
        # self.ch.get('https://creator.xiaohongshu.com/new/home')
        # self.ch.get('')

    def two_zhixing(self):

        self.xhs_huizong()
        self.ch.get('https://creator.xiaohongshu.com/new/home')

xhs = XHS_XPIDER(user_id='1111111e1211w')
aa = xhs.xhs_huizong()


# print(os.getcwd() + '/user_info_dir1')
